/**
 * 
 */
package domain;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java_persistence.*;

import junit.framework.TestCase;


/** JUnit tests to assure functionality of domain classes and database persistance.   
 * @author joseph
 *
 */
public class DomainTest extends TestCase{
	
	private SqlSession setup_database() {
		
		
		SqlSession session = null ;
		
		try {
			String myBatis_config = "resources/mybatis-config.xml";
			Reader config_reader = Resources.getResourceAsReader(myBatis_config);
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(config_reader);
			session = sessionFactory.openSession();
		}
		catch (Exception e) {
			System.out.println("Caught Exception: " + e.getMessage());
		}
		return session;

	}
	
	private boolean users_equal( User u, User v ) {
		Boolean ret = true;
		
		ret = (u.getActive_account().equals(v.getActive_account()))? ret : false;
		ret = (u.getAddress().equals(v.getAddress()))? ret : false;
		ret = (u.getEmail().equals(v.getEmail()))? ret : false;
		ret = (u.getName().equals(v.getName()))? ret: false;
		ret = (u.getPassword().equals(v.getPassword()))? ret : false;
		ret = (u.getPhone_number().equals(v.getPhone_number()))? ret : false;
		ret = (u.getUsername().equals(v.getUsername()))? ret: false;
		
		
		if( !ret ) {
			System.out.println( u.getActive_account().toString() + " = " + v.getActive_account().toString());
			System.out.println( u.getAddress() + " = " + v.getAddress() );
			System.out.println( u.getEmail() + " = " + v.getEmail() );
			System.out.println( u.getName() + " = " + v.getName() );
			System.out.println( u.getPassword() + " = " + v.getPassword());
			System.out.println( u.getPhone_number() + " = " + v.getPhone_number());
			System.out.println( u.getUsername() + " = " + v.getUsername());
		}
		
		return ret;
	}
	
	
	public void test_user_persistence() {
		SqlSession session = setup_database();
				
		//Create a user. 
		User u = new User("test1", "password1", "Test User1", "1234567890", "234 Peachtree RD Atlanta, GA 33333", "admin@google.com", true);
				
		//Save him.
		u.create(session.getMapper(UserMapper.class));
		session.commit();
		
		//Get him and check consistency. 
		User v = new User();
		
		v = session.getMapper(UserMapper.class).get_user_by_username(u.getUsername());
			
		assertTrue( users_equal(u,v) ); // will be true if users is stored and retrieved correctly.
		
		// test the update method.
		u.setEmail("not_admin@google.com");
		u.update(session.getMapper(UserMapper.class));
		session.commit();
		
		v = session.getMapper(UserMapper.class).get_user_by_username(u.getUsername());

		assertTrue( users_equal( u, v)); // will be true if user is updated and retrieved correctly.
		
		
		// Test getting a password.
		
		String u_password = session.getMapper(UserMapper.class).get_password_by_username(u.getUsername());
		
		assertTrue( u.getPassword().equals(u_password));
		
		// test the delete method.
		
		u.delete(session.getMapper(UserMapper.class));
		session.commit();
		v = session.getMapper(UserMapper.class).get_user_by_username(u.getUsername());

		assertTrue( v == null );
		

		// Tests that get_users returns properly formed User objects.
		List<User> users = new ArrayList<User>();
		users = session.getMapper(UserMapper.class).get_users();
		
		for( User i : users ) {
			assertTrue ( i instanceof User);  
			
			assertFalse( i.getAddress() == null );
			assertFalse( i.getEmail() == null );
			assertFalse( i.getName() == null );
			assertFalse( i.getPassword() == null);
			assertFalse( i.getPhone_number() == null);
			assertFalse( i.getUsername() == null );
			assertFalse( i.getActive_account() == null);
		
		}


		
		session.close();
	}
	
	
	public void test_doctor_persistence() {
		
		SqlSession session = setup_database();
		
		// Test retrieving doctors.
		
		List<Doctor> doctors = new ArrayList<Doctor>();
		doctors = session.getMapper(DoctorMapper.class).get_doctors();
		
		for( Doctor i : doctors ) {
			assertTrue( i instanceof Doctor);
		}
		
		Doctor d = new Doctor("test1", "password1", "Test User1", "1234567890", "234 Peachtree RD Atlanta, GA 33333", "admin@google.com", true);

		// Test making u a doctor.
		
		d.create(session.getMapper(UserMapper.class));
		session.commit();
		
		d.make_doctor(session.getMapper(DoctorMapper.class));
		session.commit();
		
		assertTrue( d instanceof Doctor);  // this is kind of irrelevant
		
		doctors = session.getMapper(DoctorMapper.class).get_doctors();

		boolean test_d_is_doctor = false;
		
		for( Doctor i : doctors ) {
			test_d_is_doctor = (i.getUsername().equals(d.getUsername()))? true : test_d_is_doctor;
		}
		
		assertTrue( test_d_is_doctor );
		
		// Test making u not a doctor.
		
		d.remove_doctor(session.getMapper(DoctorMapper.class));
		session.commit();
		doctors = session.getMapper(DoctorMapper.class).get_doctors();

		test_d_is_doctor = false;
		
		for( Doctor i : doctors ) {
			test_d_is_doctor = (i.getUsername().equals(d.getUsername()))? true : test_d_is_doctor;
		}
		
		assertFalse( test_d_is_doctor );
		
		//and clean it up for subsequent tests.
		
		d.delete(session.getMapper(UserMapper.class));
		session.commit();
	}
	
	public void test_nurse_persistence() {
		

		SqlSession session = setup_database();
		
		// Test retrieving nurses.

		List<Nurse> nurses = new ArrayList<Nurse>();
		nurses = session.getMapper(NurseMapper.class).get_nurses();
		
		for( Nurse i : nurses ) {
			assertTrue( i instanceof Nurse);
		}

	
		Nurse d = new Nurse("test1", "password1", "Test User1", "1234567890", "234 Peachtree RD Atlanta, GA 33333", "admin@google.com", true);

		// Test making u a nurse.


		
		d.create(session.getMapper(UserMapper.class));
		session.commit();
		
		
		d.make_nurse(session.getMapper(NurseMapper.class));
		session.commit();
		
		assertTrue( d instanceof Nurse);  // this is kind of irrelevant

		nurses = session.getMapper(NurseMapper.class).get_nurses();

		boolean test_d_is_nurse = false;
		
		for( Nurse i : nurses ) {
			test_d_is_nurse = (i.getUsername().equals(d.getUsername()))? true : test_d_is_nurse;
		}
		
		assertTrue( test_d_is_nurse );

		// Test making u not a nurse.
		
		d.delete_nurse(session.getMapper(NurseMapper.class));
		session.commit();

		nurses = session.getMapper(NurseMapper.class).get_nurses();

		test_d_is_nurse = false;
		
		for( Nurse i : nurses ) {
			test_d_is_nurse = (i.getUsername().equals(d.getUsername()))? true : test_d_is_nurse;
		}
		
		assertFalse( test_d_is_nurse );
		
		//System.out.println("Failure was after this.");
		
		//and clean it up for subsequent tests.
		
		d.delete(session.getMapper(UserMapper.class));
		session.commit();
		
		
		session.close();
		
	}
	

	
	public void test_sysadmin_persistence() {

		SqlSession session = setup_database();
		
		// Test retrieving sysadmins.

		List<SystemAdministrator> sysadmins = new ArrayList<SystemAdministrator>();
		sysadmins = session.getMapper(SystemAdministratorMapper.class).get_sysadmins();
		
		for( SystemAdministrator i : sysadmins ) {
			assertTrue( i instanceof SystemAdministrator);
		}

	
		SystemAdministrator sa = new SystemAdministrator("test1", "password1", "Test User1", "1234567890", "234 Peachtree RD Atlanta, GA 33333", "admin@google.com", true);

		// Test making u a sysadmin.
	
		sa.create(session.getMapper(UserMapper.class));
		session.commit();
		
		
		sa.create_sysadmin(session.getMapper(SystemAdministratorMapper.class));
		session.commit();
		
		assertTrue( sa instanceof SystemAdministrator);  // this is kind of irrelevant

		sysadmins = session.getMapper(SystemAdministratorMapper.class).get_sysadmins();

		boolean test_d_is_sysadmin = false;
		
		for( SystemAdministrator i : sysadmins ) {
			test_d_is_sysadmin = (i.getUsername().equals(sa.getUsername()))? true : test_d_is_sysadmin;
		}
		
		assertTrue( test_d_is_sysadmin );
		
		SystemAdministrator sb = new SystemAdministrator();
		sb.get_sa_by_username(sa.getUsername(), session.getMapper(SystemAdministratorMapper.class));
		
		boolean match = true;
		
		match = ( sa.getUsername().equals(sb.getUsername()))? match : false ;
		match = ( sa.getAddress().equals(sb.getAddress()))? match : false ;
		match = ( sa.getEmail().equals(sb.getEmail()))? match : false ;
		match = ( sa.getName().equals(sb.getName()))? match : false ;
		match = ( sa.getPassword().equals(sb.getPassword()))? match : false ;
		match = ( sa.getPhone_number().equals(sb.getPhone_number()))? match : false ;
		match = ( sa.getActive_account().equals(sb.getActive_account()))? match : false ;
		
		assertTrue(match);
		
		// Test making u not a nurse.
		
		sa.delete_sysadmin(session.getMapper(SystemAdministratorMapper.class));
		session.commit();

		sysadmins = session.getMapper(SystemAdministratorMapper.class).get_sysadmins();

		test_d_is_sysadmin = false;
		
		for( SystemAdministrator i : sysadmins ) {
			test_d_is_sysadmin = (i.getUsername().equals(sa.getUsername()))? true : test_d_is_sysadmin;
		}
		
		assertFalse( test_d_is_sysadmin );
		
	
		//and clean it up for subsequent tests.
		
		sa.delete(session.getMapper(UserMapper.class));
		session.commit();
		
		session.close();
		
	}
	
	
	public void test_services_persistence() {
		
		SqlSession session = setup_database();
		
		Services s = new Services( "Test Service", (float) 135.45 );
		
		s.create( session.getMapper(ServicesMapper.class));
		session.commit();
		
		Services t = new Services();
		
		t = Services.get_service_by_name(s.getService_name(), session.getMapper(ServicesMapper.class));
		
		assertTrue( t instanceof Services);
		
		boolean match = true;
		
		match = ( s.getService_name().equals(t.getService_name()) )? match: false;
		match = ( s.getPrice() == t.getPrice() )? match: false;
		
		assertTrue( match );
		
		s.setPrice( (float) 54.10 );
		
		s.update_price( session.getMapper(ServicesMapper.class) );
		session.commit();
		
		t = Services.get_service_by_name(s.getService_name(), session.getMapper(ServicesMapper.class));
		
		match = ( s.getService_name().equals(t.getService_name()) )? match: false;
		match = ( s.getPrice() == t.getPrice() )? match: false;
		
		assertTrue( match );
		
		s.delete( session.getMapper(ServicesMapper.class) );
		session.commit();
		
		t = Services.get_service_by_name(s.getService_name(), session.getMapper(ServicesMapper.class));

		assertTrue( t == null );
		
		List<String> service_names = new ArrayList<String>();
		
		service_names = session.getMapper(ServicesMapper.class).get_all_service_names();
		
		for( String i : service_names) {
			assertTrue( i instanceof String);
			assertFalse( i.equals(null));
		}
		
		List<Services> services = new ArrayList<Services>();
		
		for( String i: service_names) {
			services.add( session.getMapper(ServicesMapper.class).get_service_by_name(i) );
		}
		
		for( Services i: services) {
			assertTrue( i instanceof Services);
			assertFalse( i.getService_name().equals(null));
			assertFalse( (Float) i.getPrice() == null);
		}
		
		/*
		 * Note: ServicesMapper.retrieve_by_appointment(...) will be tested as part of the tests on Appointment.
		 * It doesn't make any sense to test it here.
		 */
		
		session.close();
		
	}
	
	public void test_patient_persistence() {

		SqlSession session = setup_database();
		
		Patient p = new Patient("test1", "password1", "Test User1", "1234567890", "234 Peachtree RD Atlanta, GA 33333", "admin@google.com", true,
								"Test RX", "Test Insurance Company", "none", true, "2980-02-18");

		p.create( session.getMapper(UserMapper.class));
		p.create_patient( session.getMapper(PatientMapper.class));
		session.commit();
		
		Patient q = new Patient();
		
		q = session.getMapper(PatientMapper.class).get_patient_by_username(p.getUsername());
		
		boolean pq = true;
		
		pq = ( p.getIs_male() == q.getIs_male() )? pq : false;
		pq = ( p.getActive_account().equals(q.getActive_account()))? pq : false;
		pq = ( p.getAddress().equals(q.getAddress()))? pq : false;
		pq = ( p.getAllergies().equals(q.getAllergies()))? pq : false;
		pq = ( p.getBirthdate().equals(q.getBirthdate()))? pq : false;
		pq = ( p.getEmail().equals(q.getEmail()))? pq : false;
		pq = ( p.getInsurance_info().equals(q.getInsurance_info()))? pq : false;
		pq = ( p.getName().equals(q.getName()))? pq : false;
		pq = ( p.getPassword().equals(q.getPassword()))? pq : false;
		pq = ( p.getPharmacy().equals(q.getPharmacy()))? pq : false;
		pq = ( p.getPhone_number().equals(q.getPhone_number()))? pq : false;
		pq = ( p.getUsername().equals(q.getUsername()))? pq: false;
		
		assertTrue( pq );
		
		p.setPhone_number("0987654321");
		p.setAllergies("Penicilin");
		
		p.update( session.getMapper(UserMapper.class));
		p.update_patient( session.getMapper(PatientMapper.class));
		session.commit();

		q = session.getMapper(PatientMapper.class).get_patient_by_username(p.getUsername());

		pq = true;
		
		pq = ( p.getIs_male() == q.getIs_male() )? pq : false;
		pq = ( p.getActive_account().equals(q.getActive_account()))? pq : false;
		pq = ( p.getAddress().equals(q.getAddress()))? pq : false;
		pq = ( p.getAllergies().equals(q.getAllergies()))? pq : false;
		pq = ( p.getBirthdate().equals(q.getBirthdate()))? pq : false;
		pq = ( p.getEmail().equals(q.getEmail()))? pq : false;
		pq = ( p.getInsurance_info().equals(q.getInsurance_info()))? pq : false;
		pq = ( p.getName().equals(q.getName()))? pq : false;
		pq = ( p.getPassword().equals(q.getPassword()))? pq : false;
		pq = ( p.getPharmacy().equals(q.getPharmacy()))? pq : false;
		pq = ( p.getPhone_number().equals(q.getPhone_number()))? pq : false;
		pq = ( p.getUsername().equals(q.getUsername()))? pq: false;
		
		assertTrue( pq );
		
		p.delete_patient( session.getMapper(PatientMapper.class));
		session.commit();
		p.delete(session.getMapper(UserMapper.class));
		session.commit();
		
		q = session.getMapper(PatientMapper.class).get_patient_by_username(p.getUsername());
		
		assertTrue ( q == null);

		List<Patient> patients = new ArrayList<Patient>();
		
		patients=session.getMapper(PatientMapper.class).get_patients();
		
		for( Patient i : patients) {
			assertTrue( i instanceof Patient);
			assertFalse( i.getUsername() == null );
		}
	}


	/*
	 * This is where it gets screwy.  There's a lot of code reuse here, but there's not really another way to test
	 * this kind of thing.  The objects we're testing here can't really exist without the stuff that they're members
	 * of.  Well, they can as POJOs, but the point is to test the interactions and especially the persistence,
	 * whcih depends on a lot of other stuff.  
	 */
	
	public void test_appointment_persistence() {

		SqlSession session = setup_database();
		
		Doctor d = new Doctor( "docTest1", "docPass", "Doctor Test1", "1234567890", "Some Address", "doctor@derp.com", true);
		d.create(session.getMapper(UserMapper.class));
		d.make_doctor(session.getMapper(DoctorMapper.class));
		
		Patient p = new Patient( "patTest1", "patPass", "Patient Test1", "1234567890", "Another Address", "patient@derp.com", true,
								 "A Pharmacy", "An Insurance Carrier", "Penicilin", true, "1980-05-21");
		p.create(session.getMapper(UserMapper.class));
		p.create_patient(session.getMapper(PatientMapper.class));
		
		Nurse n = new Nurse( "nurTest1", "nurPass", "Nurse Test1", "1234567890", "Yet Another Address", "nurse@derp.com", true);
		n.create(session.getMapper(UserMapper.class));
		n.make_nurse(session.getMapper(NurseMapper.class));

		//( Doctor d, Nurse n, int bps, int bpd, int p, float t, int hi, int wp, String di, String no, Orders o, Invoice i) {
		TreatmentRecord tr = new TreatmentRecord( d, n, 180, 80, 60, (float) 98.6, 60, 180, "No diagnosis", "No Notes.", null, null);

		String date = "2011-06-15 14:00:00";
		// String d, String c, Doctor rd, Patient p, TreatmentRecord tr
		Appointment a = new Appointment( date, "Insomnia", d, p, tr);
		
		
		a.create( session.getMapper(AppointmentMapper.class) );
		session.commit();
		
		tr.create_treatment_record(a, session);
		session.commit(); 
		
		Appointment a2 = new Appointment();
		
		a2.retrieve(date, p.getUsername(), session);
		
		boolean match = true;
		
		//these work
		match = ( a.getComplaint().equals(a2.getComplaint()) )? match: false;
		match = ( a.getDate().equals(a2.getDate()))? match: false;
		match = ( a.getPatient().getUsername().equals(a2.getPatient().getUsername()))? match: false;
		match = ( a.getRequested_doctor().getUsername().equals(a2.getRequested_doctor().getUsername()))? match: false;

		assertTrue( match );
		
		tr.delete_treatment_record(a, session.getMapper(TreatmentRecordMapper.class));
		session.commit();
		a.delete( session.getMapper(AppointmentMapper.class));
		session.commit();
		
		Appointment a3 = new Appointment();
		
		a3.retrieve(date, p.getUsername(), session);
		
		assertTrue( a3.getComplaint() == null );
		
		// and cleanup.
		
		d.remove_doctor(session.getMapper(DoctorMapper.class));
		n.delete_nurse(session.getMapper(NurseMapper.class));
		p.delete_patient(session.getMapper(PatientMapper.class));
		session.commit();
		
		d.delete(session.getMapper(UserMapper.class));
		n.delete(session.getMapper(UserMapper.class));
		p.delete(session.getMapper(UserMapper.class));
		session.commit();
		
		session.close();
	}
	
	public void test_treatment_record() {
		
		SqlSession session = setup_database();
		
		Doctor d = new Doctor( "docTest1", "docPass", "Doctor Test1", "1234567890", "Some Address", "doctor@derp.com", true);
		d.create(session.getMapper(UserMapper.class));
		d.make_doctor(session.getMapper(DoctorMapper.class));
		
		Patient p = new Patient( "patTest1", "patPass", "Patient Test1", "1234567890", "Another Address", "patient@derp.com", true,
								 "A Pharmacy", "An Insurance Carrier", "Penicilin", true, "1980-05-21");
		p.create(session.getMapper(UserMapper.class));
		p.create_patient(session.getMapper(PatientMapper.class));
		
		Nurse n = new Nurse( "nurTest1", "nurPass", "Nurse Test1", "1234567890", "Yet Another Address", "nurse@derp.com", true);
		n.create(session.getMapper(UserMapper.class));
		n.make_nurse(session.getMapper(NurseMapper.class));

		//( Doctor d, Nurse n, int bps, int bpd, int p, float t, int hi, int wp, String di, String no, Orders o, Invoice i) {
		TreatmentRecord tr = new TreatmentRecord( d, n, 180, 80, 60, (float) 98.6, 60, 180, "No diagnosis", "No Notes.", null, null);

		String date = "2011-06-15 14:00:00";
		// String d, String c, Doctor rd, Patient p, TreatmentRecord tr
		Appointment a = new Appointment( date, "Insomnia", d, p, tr);
		
		
		a.create( session.getMapper(AppointmentMapper.class) );
		session.commit();
		
		tr.create_treatment_record(a, session);
		session.commit(); 
		
		
		// We've already tested a lot of the TreatmentRecord functionality, so here's the rest of it...updating.
		tr.setNotes("After Update Test");
		
		tr.update_treatment_record(a, session.getMapper(TreatmentRecordMapper.class));
		session.commit();
		
		TreatmentRecord tr2 = new TreatmentRecord();
		
		tr2.retrieve(a.getDate(), p.getUsername(), session);
		
		boolean match = true;
		
		match = ( tr2.getAttending_doctor().getUsername().equals(d.getUsername())) ? match : false;
		match = ( tr2.getAttending_nurse().getUsername().equals(n.getUsername())) ? match : false;
		match = ( tr2.getBp_diastolic() == tr.getBp_diastolic()) ? match: false;
		match = ( tr2.getBp_systolic() == tr.getBp_systolic()) ? match: false;
		match = ( tr2.getDiagnosis().equals(tr.getDiagnosis())) ? match : false;
		match = ( tr2.getHeight_inches() == tr.getHeight_inches()) ? match : false;
		match = ( tr2.getNotes().equals(tr.getNotes())) ? match: false;
		match = ( tr2.getPulse() == tr.getPulse()) ? match: false;
		match = ( tr2.getTemperature() == tr.getTemperature()) ? match: false;
		match = ( tr2.getWeight_pounds() == tr.getWeight_pounds()) ? match: false;
		
		assertTrue(match);

		// and cleanup.
		tr.delete_treatment_record(a, session.getMapper(TreatmentRecordMapper.class));
		session.commit();
		a.delete( session.getMapper(AppointmentMapper.class));
		session.commit();
		
		d.remove_doctor(session.getMapper(DoctorMapper.class));
		n.delete_nurse(session.getMapper(NurseMapper.class));
		p.delete_patient(session.getMapper(PatientMapper.class));
		session.commit();
		
		d.delete(session.getMapper(UserMapper.class));
		n.delete(session.getMapper(UserMapper.class));
		p.delete(session.getMapper(UserMapper.class));
		session.commit();
		
		session.close();
		
	}
	
	public void test_orders_persistence () {
		
		SqlSession session = setup_database();
		
		Doctor d = new Doctor( "docTest1", "docPass", "Doctor Test1", "1234567890", "Some Address", "doctor@derp.com", true);
		d.create(session.getMapper(UserMapper.class));
		d.make_doctor(session.getMapper(DoctorMapper.class));
		
		Patient p = new Patient( "patTest1", "patPass", "Patient Test1", "1234567890", "Another Address", "patient@derp.com", true,
								 "A Pharmacy", "An Insurance Carrier", "Penicilin", true, "1980-05-21");
		p.create(session.getMapper(UserMapper.class));
		p.create_patient(session.getMapper(PatientMapper.class));
		
		Nurse n = new Nurse( "nurTest1", "nurPass", "Nurse Test1", "1234567890", "Yet Another Address", "nurse@derp.com", true);
		n.create(session.getMapper(UserMapper.class));
		n.make_nurse(session.getMapper(NurseMapper.class));

		Orders o = new Orders("No Prescription", "No Lab Work", "no followup instructions", "no other instructions"); //wow, i'm creative.
		
		//( Doctor d, Nurse n, int bps, int bpd, int p, float t, int hi, int wp, String di, String no, Orders o, Invoice i) {
		TreatmentRecord tr = new TreatmentRecord( d, n, 180, 80, 60, (float) 98.6, 60, 180, "No diagnosis", "No Notes.", o, null);

		String date = "2011-06-15 14:00:00";
		// String d, String c, Doctor rd, Patient p, TreatmentRecord tr
		Appointment a = new Appointment( date, "Insomnia", d, p, tr);
		
		a.create( session.getMapper(AppointmentMapper.class) );
		session.commit();
		
		tr.create_treatment_record(a, session);
		session.commit(); 
		
		o.create_orders(tr, a, session.getMapper(OrdersMapper.class));
		session.commit();
		
		Orders o2 = new Orders();
		
		o2.retrieve(tr, a, session.getMapper(OrdersMapper.class));
		
		boolean match = true;
		
		match = (o.getFollup_instructions().equals(o2.getFollup_instructions())) ? match : false;
		match = (o.getLabWork().equals(o2.getLabWork())) ? match : false;
		match = (o.getOther_instructions().equals(o2.getOther_instructions()))? match : false;
		match = (o.getPrescription().equals(o2.getPrescription()))? match: false;
		assertTrue(match);
		
		o.delete_orders(tr, a, session.getMapper(OrdersMapper.class));
		
		o2.retrieve(tr, a, session.getMapper(OrdersMapper.class));
		
		boolean empty = true;
		
		empty = ( o2.getFollup_instructions() == null ) ? empty : false;
		empty = ( o2.getLabWork() == null ) ? empty : false;
		empty = ( o2.getOther_instructions() == null ) ? empty: false;
		empty = ( o2.getPrescription() == null ) ? empty : false ;
		
		assertTrue(empty);
		
		// and cleanup.
		tr.delete_treatment_record(a, session.getMapper(TreatmentRecordMapper.class));
		session.commit();
		a.delete( session.getMapper(AppointmentMapper.class));
		session.commit();
		
		d.remove_doctor(session.getMapper(DoctorMapper.class));
		n.delete_nurse(session.getMapper(NurseMapper.class));
		p.delete_patient(session.getMapper(PatientMapper.class));
		session.commit();
		
		d.delete(session.getMapper(UserMapper.class));
		n.delete(session.getMapper(UserMapper.class));
		p.delete(session.getMapper(UserMapper.class));
		session.commit();
		
		session.close();
		
	}
	
	public void test_invoice_persistence() {
		SqlSession session = setup_database();
		
		Doctor d = new Doctor( "docTest1", "docPass", "Doctor Test1", "1234567890", "Some Address", "doctor@derp.com", true);
		d.create(session.getMapper(UserMapper.class));
		d.make_doctor(session.getMapper(DoctorMapper.class));
		
		Patient p = new Patient( "patTest1", "patPass", "Patient Test1", "1234567890", "Another Address", "patient@derp.com", true,
								 "A Pharmacy", "An Insurance Carrier", "Penicilin", true, "1980-05-21");
		p.create(session.getMapper(UserMapper.class));
		p.create_patient(session.getMapper(PatientMapper.class));
		
		Nurse n = new Nurse( "nurTest1", "nurPass", "Nurse Test1", "1234567890", "Yet Another Address", "nurse@derp.com", true);
		n.create(session.getMapper(UserMapper.class));
		n.make_nurse(session.getMapper(NurseMapper.class));

		Orders o = new Orders("No Prescription", "No Lab Work", "no followup instructions", "no other instructions"); //wow, i'm creative.
		
		//( Doctor d, Nurse n, int bps, int bpd, int p, float t, int hi, int wp, String di, String no, Orders o, Invoice i) {
		TreatmentRecord tr = new TreatmentRecord( d, n, 180, 80, 60, (float) 98.6, 60, 180, "No diagnosis", "No Notes.", o, null);

		String date = "2011-06-15 14:00:00";
		// String d, String c, Doctor rd, Patient p, TreatmentRecord tr
		Appointment a = new Appointment( date, "Insomnia", d, p, tr);
		
		a.create( session.getMapper(AppointmentMapper.class) );
		session.commit();
		
		tr.create_treatment_record(a, session);
		session.commit(); 
		
		o.create_orders(tr, a, session.getMapper(OrdersMapper.class));
		session.commit();
		
		Invoice in = new Invoice();

		Services s1 = new Services("Test Service 1", (float) 40.00);
		s1.create(session.getMapper(ServicesMapper.class));
		Services s2 = new Services("Test Service 2", (float) 185.50);
		s2.create(session.getMapper(ServicesMapper.class));
		Services s3 = new Services("Test Service 3", (float) 9.99);
		s3.create(session.getMapper(ServicesMapper.class));
		session.commit();
		
		in.addService(s1);
		in.addService(s2);
		in.addService(s3);
		
		tr.setInvoice(in);
		tr.update_treatment_record(a, session.getMapper(TreatmentRecordMapper.class));
		session.commit();
		
		TreatmentRecord tr2 = new TreatmentRecord();
		tr2.retrieve(date, p.getUsername(), session);
		
		Invoice in2 = new Invoice();
		in2.get_services(a, session.getMapper(ServicesMapper.class));
		
		for( int i = 0; i < in.getMyServices().size() && i < in2.getMyServices().size(); i++) {
			assertTrue( in.getMyServices().get(i).getPrice() == in2.getMyServices().get(i).getPrice() );
			assertTrue( in.getMyServices().get(i).getService_name().equals(in2.getMyServices().get(i).getService_name()));
		}
		
		
		in.delete_invoice(a, session.getMapper(ServicesMapper.class));
		session.commit();
		
		in2.get_services(a, session.getMapper(ServicesMapper.class) );
		
		assertTrue( in2.getMyServices().size()==0 );
		
		// and cleanup.
		s1.delete(session.getMapper(ServicesMapper.class));
		s2.delete(session.getMapper(ServicesMapper.class));
		s3.delete(session.getMapper(ServicesMapper.class));
		session.commit();

		o.delete_orders(tr, a, session.getMapper(OrdersMapper.class));
		session.commit();
		
		tr.delete_treatment_record(a, session.getMapper(TreatmentRecordMapper.class));
		session.commit();
		
		a.delete( session.getMapper(AppointmentMapper.class));
		session.commit();
	
		d.remove_doctor(session.getMapper(DoctorMapper.class));
		n.delete_nurse(session.getMapper(NurseMapper.class));
		p.delete_patient(session.getMapper(PatientMapper.class));
		session.commit();
		d.delete(session.getMapper(UserMapper.class));
		n.delete(session.getMapper(UserMapper.class));
		p.delete(session.getMapper(UserMapper.class));
		session.commit();
		session.close();
	}
	
	
}
